package com.gmail.dmitryustimov.ms.java7.observer.classic.testperf;

import com.gmail.dmitryustimov.ms.java7.observer.classic.demo.subj1subsc1event1.*;

import java.util.ArrayList;
import java.util.List;

public class TestPerfSubj1Subsc1Event1 {

    private static final int NUM_SUBSCRIBERS = 1;
    private static final int NUM_UPDATES = 100000;
    private static final int NUM_WARM_UP = 3;
    private static final int NUM_TESTS = 1000;

    private static final String MESSAGE = "observer_classic, 1 subject, 1 subscriber, 1 event type, took: ";

    public static void main(String[] args) {

        //warm up
        for (int i = 0; i < NUM_WARM_UP; i++) {
            test();
        }

        //tests
        long time = 0;
        for (int j = 0; j < NUM_TESTS; j++) {
            time += test();
        }
        System.out.println("final - " + MESSAGE + time/NUM_TESTS + "ns (" + (time/NUM_TESTS)/1000000 + "ms)");
    }

    private static long test() {
        Subject subject = new Subject();

        for (long i = 0; i < NUM_SUBSCRIBERS; i++) {
            new Subscriber(subject, "Subscriber" + i);
        }

        long startTime = System.nanoTime();

        for (long j = 0; j < NUM_UPDATES; j++) {
            subject.makeEvent();
        }

        long estimatedTime = System.nanoTime() - startTime;
        System.out.println(MESSAGE + estimatedTime + "ns");
        return estimatedTime;
    }
}
